package Student;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class S_selstu extends JPanel implements ActionListener {
    JTextField 学号,姓名,导师;
    JButton 查找;
    JTextField 课号,课名;
    JButton 查找1;
    JTextField 课号1,学号1,成绩;
    JButton 查找2;
    public S_selstu(){
        学号=new JTextField(12);
        姓名=new JTextField(12);
        导师=new JTextField(12);
        课号=new JTextField(12);
        课名=new JTextField(12);
        课号1=new JTextField(12);
        学号1=new JTextField(12);
        成绩=new JTextField(12);
        查找=new JButton("查找学生信息");
        查找1=new JButton("查找课程信息");
        查找2=new JButton("查找选课信息");
        Box box1=Box.createHorizontalBox();//横放 box
        Box box2=Box.createHorizontalBox();
        Box box3=Box.createHorizontalBox();
        Box box4=Box.createHorizontalBox();
        Box box5=Box.createHorizontalBox();
        Box box6=Box.createHorizontalBox();
        box1.add(new JLabel("学号:",JLabel.CENTER));
        box1.add(学号);
        box1.add(new JLabel("姓名:",JLabel.CENTER));
        box1.add(姓名);
        box2.add(查找);
        box3.add(new JLabel("课号:",JLabel.CENTER));
        box3.add(课号);
        box3.add(new JLabel("课名:",JLabel.CENTER));
        box3.add(课名);
        box5.add(查找1);
        box4.add(new JLabel("课号:",JLabel.CENTER));
        box4.add(课号1);
        box4.add(new JLabel("学号:",JLabel.CENTER));
        box4.add(学号1);
        box4.add(new JLabel("成绩:",JLabel.CENTER));
        box4.add(成绩);
        box6.add(查找2);
        Box boxH1=Box.createVerticalBox();//竖放 box
        boxH1.add(box1);
        boxH1.add(box2);
        boxH1.add(Box.createVerticalGlue());
        Box boxH2=Box.createVerticalBox();//竖放 box
        boxH2.add(box3);
        boxH2.add(box5);
        boxH2.add(Box.createVerticalGlue());
        Box boxH3=Box.createVerticalBox();//竖放 box
        boxH3.add(box4);
        boxH3.add(box6);
        boxH3.add(Box.createVerticalGlue());
        查找.addActionListener(this);
        查找1.addActionListener(this);
        查找2.addActionListener(this);
        JPanel messPanel=new JPanel();
        JPanel picPanel=new JPanel();
        JPanel threePanel=new JPanel();
        messPanel.add(boxH1);
        picPanel.add(boxH2);
        threePanel.add(boxH3);
        setLayout(new BorderLayout());
        JSplitPane splitV=new
                JSplitPane(JSplitPane.VERTICAL_SPLIT,messPanel,picPanel);//分割
        add(splitV,BorderLayout.CENTER);
        JSplitPane splitV1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,splitV,threePanel);//分割
        add(splitV1,BorderLayout.CENTER);
        validate();
    }
    public void actionPerformed(ActionEvent c){
        Object obj=c.getSource();
        Statement stmt=null;
        ResultSet rs=null;
        int row=0;
        int i=0;
        String sql=null;
        S_students K;
        S_selc K1;
        S_selsc K2;
        if(obj==查找){
            if( 学号 .getText().equals("")&& 姓名 .getText().equals("")){
                sql="select * from Student ";System.out.print("000");//000
            }

            else{
                if(学号.getText().equals("")){
                    if(姓名.getText().equals("")){
                        sql="select * from Student where Student_teacher like'%"+ 导师.getText()+"%'";System.out.print("001");
                    }
                    else{if(导师.getText().equals("")){
                        sql="select * from Student where Student_name like'%"+姓名.getText()+"%'";System.out.print("010");
                    }
                    else{
                        sql="select * from Student where Student_name like'%"+ 姓名.getText()+"%'and Student_teacher like'%"+导师.getText()+"%'";
                        //System.out.print("011");
                    }

                    }
                }
                else{if(姓名.getText().equals("")){
                    if(导师.getText().equals("")){sql="select * from Student where Student_id like'%"+学号.getText()+"%'";
                        //System.out.print("100");
                        }
                    else{sql="select * from Student where Student_id like'%"+学号.getText()+"%' and Student_teacher like'%"+导师.getText()+"%'";
                      //  System.out.print("101");
                    }}
                else{if(导师.getText().equals("")){sql="select * from Student where Student_id like'%"+学号.getText()+"%' and Student_name like'%"+姓名.getText()+"%'";
                   // System.out.print("110");
                }
                else{sql="select * from Student where Student_id like'%"+ 学号 .getText()+"%' and Student_name like'%"+ 姓名 .getText()+"%' and Student_teacher like'%"+ 导师.getText()+"%'";
                   // System.out.print("111");
                }}}
            }
            K=new S_students(sql);
        }


        else{if(obj==查找1){
            if(课号.getText().equals("")&&课名.getText().equals("")){
                sql="select * from Course ";System.out.print("00");//00
            }
            else{
                if(课号.getText().equals("")){sql="select * from Course where Course_name like'%"+课名.getText()+"%'";System.out.print("01");}
                else{if(导师.getText().equals("")){sql="select * from Course where Course_id like'%"+
                        课号.getText()+"%'";System.out.print("10");}
                else{sql="select * from Course where Course_id like'%"+课号.getText()+"%' and Course_name like'%"+姓名.getText()+"%'";System.out.print("11");}
                }
            }
            K1=new S_selc(sql);}


        else{if(obj==查找2){
            if( 课号1.getText().equals("")&& 学号1.getText().equals("")&& 成绩.getText().equals("")){
                sql="select Student_Course.Course_id,Course_name,Student_Course.Student_id,Student_name,Score from Student_Course,Course,Student where Course.Course_id=Student_Course.Course_id and Student.Student_id=Student_Course.Student_id";System.out.print("000");//000
            }
            else{
                if(课号1.getText().equals("")){
                    if(学号1.getText().equals("")){
                        sql="select dbo.Student_Course.Course_id,Course_name,Student_Course.Student_id,Student_name,Score from Student_Course,Course,Student where Score like'%"+成绩.getText()+"%' and Course.Course_id=Student_Course.Course_id and Student.Student_id=Student_Course.Student_id";System.out.print("001");}
                    else{if( 成绩 .getText().equals("")){sql="select Student_Course.Course_id,Course_name,Student_Course.Student_id,Student_name,Score from Student_Course,Course,Student where Student_Course.Student_id like'%"+学号1.getText()+"%' and Course.Course_id=Student_Course.Course_id and Student.Student_id=Student_Course.Student_id";System.out.print("010");}
                    else{sql="select Student_Course.Course_id,Course_name,Student_Course.Student_id,Student_name,Score from Student_Course,Course,Student where Student_Course.Student_id like'%"+ 学号1.getText()+"%'and Score like'%"+ 成绩 .getText()+"%' and Course.Course_id=Student_Course.Course_id and Student.Student_id=Student_Course.Student_id";System.out.print("011");}}}
                else{if(学号1.getText().equals("")){
                    if( 成绩 .getText().equals("")){sql="select Student_Course.Course_id,Course_name,Student_Course.Student_id,Student_name,Score from Student_Course,Course,Student where Student_Course.Course_id like'%"+课号1.getText()+"%' and Course.Course_id=Student_Course.Course_id and Student.Student_id=Student_Course.Student_id";System.out.print("100");}
                    else{sql="select Student_Course.Course_id,Course_name,Student_Course.Student_id,Student_name,Score from Student_Course,Course,Student where Student_Course.Course_id like'%"+ 课号1.getText()+"%' and Score like'%"+ 成绩 .getText()+"%' and Course.Course_id=Student_Course.Course_id and Student.Student_id=Student_Course.Student_id";System.out.print("101");}}
                else{if( 成绩 .getText().equals("")){sql="select Student_Course.Course_id,Course_name,Student_Course.Student_id,Student_name,Score from Student_Course,Course,Student where Student_Course.Course_id like'%"+课号1.getText()+"%' and Student_Course.Student_id like'%"+ 学号1.getText()+"%' and Course.Course_id=Student_Course.Course_id and Student.Student_id=Student_Course.Student_id";System.out.print("110");}
                else{sql="select Student_Course.Course_id,Course_name,Student_Course.Student_id,Student_name,Score from Student_Course,Course,Student where Student_Course.Course_id like'%"+课号1.getText()+"%' and Student_Course.Student_id like'%"+学号1.getText()+"%' and Score like'%"+成绩.getText()+"%' and Course.Course_id=Student_Course.Course_id and Student.Student_id=Student_Course.Student_id";System.out.print("111");}}}
            }
            K2=new S_selsc(sql);
        }
        }
        }
    }
    // 连接数据库方法
    public static Connection CONN(){
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加 载JDBC 驱动
        String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=CMS"; //连接服务器和数据库 test
        String userName = "sa"; //默认用户名
        String userPwd = "123456"; //密码
        Connection dbConn=null;
        try {
            Class.forName(driverName);
            dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
            System.out.println("Connection Successful!"); //如果连接成功 控制台 输出Connection Successful!
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dbConn;
    }
}
